package cn.stylefeng.guns;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.RowUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.Builder;
import lombok.Data;
import lombok.extern.java.Log;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellUtil;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Set;

@Controller
public class ElevenController {
    private final static String token = "504b6c09e4414588aa56225e880a7cb01664190637456";
    private final static String url = "https://cyoaliapi.rtidl.com/coron/api/ticketSourceResult/ticketSourceResultList.json";


    public static void main(String[] args) {
//        test();
        String res = getKuaiS("快11-", "46");
        test(res,"快11-");
        String res2 = getKuaiS("幸运11-", "58");
        test(res2,"幸运11-");
    }

    public static String getKuaiS(String fileName, String id) {
        JSONObject params = new JSONObject();
        params.put("ticketId", id);
        params.put("num", "2000");
        String res = HttpUtil.createPost(url)
                .header("token", token)
                .body(params.toJSONString())
                .execute().body();

        JSONObject jsonObject = JSONObject.parseObject(res);
        String issue = jsonObject.getJSONArray("data").getJSONObject(0).getString("issue");

        String path = StrUtil.format("D:\\乱七八糟\\快双\\\\{}{}.txt", fileName, issue.substring(6));
        System.out.println("path:" + path);
        File file = new File(path);
        FileUtil.writeString(res, file, Charset.defaultCharset());
        return res;
    }

    public static void test(String s, String fileName) {
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray datas = jsonObject.getJSONArray("data");

        for (int i = 0; i < datas.size(); i++) {
            JSONObject data = datas.getJSONObject(i);
            String issue = data.getString("issue");
            String codesStr = data.getString("code");
            String[] codes = codesStr.split(StrUtil.SPACE);
            System.out.println(StrUtil.format("{}\t{}\t{}\t{}\t{}\t{}", issue, codes[0], codes[1],
                    codes[2], codes[3], codes[4]));
        }
    }
}
